<script type="text/javascript" src="<?php echo base_url(); ?>js/calendar.js"></script>
<link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>css/dhtmlgoodies_calendar.css" />
<script type="text/javascript" >
    $(document).ready(function() {
        $('#tblRounds tr').click(function() {
            $('#tblRounds tr').each(function() {
                if ($(this).hasClass('row_selected'))
                    $(this).toggleClass('row_selected');
            })
            $(this).toggleClass('row_selected');
        });

        $('.scroll-pane').jScrollPane();
        $('.scroll-pane-arrows').jScrollPane(
            {
                showArrows: true,
                horizontalGutter: 10
            }
        );
        $('.scroll-pane-arrows-big-grid').jScrollPane(
            {
                showArrows: true,
                horizontalGutter: 10
            }
        );
		
        function errorFormRounds()
        {
            var error = '';
            var valor = '';
            
            valor = document.getElementById('nameRound').value;
            if(valor == null || valor.length == 0 || /^\s+$/.test(valor))
                return "The Name field is required";

            valor = document.getElementById('startDateRound').value;
            if(valor == null || valor.length == 0 || /^\s+$/.test(valor))
                return "The Start Date field is required";

            valor = document.getElementById('endDateRound').value;
            if(valor == null || valor.length == 0 || /^\s+$/.test(valor))
                return "The End Date field is required";

            return error;
        }
		
		 $("#new_round").dialog(
            {
              autoOpen: false,
              modal: true,
              width: 'auto',
              heigth: 'auto',
              buttons:{
                Save: function() {
                   	var errorValidacion = errorFormRounds();
                    if(errorValidacion == '') {
                        // Si los datos requeridos fueron enviados, tengo antes de continuar, que
                        // validar que las nuevas fechas de las rondas sean válidas: el rango de fechas
                        // debe poder comprender los comentarios ya cargados a la ronda.
                        var fecha_desde = document.getElementById('startDateRound').value;
                        var fecha_hasta = document.getElementById("endDateRound").value;
                        var id_round = document.getElementById("id_round").value;
                        $.ajax({
                             type: 'POST',
                             //Realizaremos la petición al metodo validUsername del controlador User
                             url: '<?php echo site_url(); ?>round/valid_range_dates',
                             data:  "fecha_desde="+fecha_desde+"&fecha_hasta="+fecha_hasta+"&id_round="+id_round,
                             success: function(resp) {
                                if(resp == '1') round_form.submit();
                                else document.getElementById('round_form_msg').innerHTML = 'There are inputs for the round with dates outside the range.';
                             }
                         });
                    }
                    else {
                        document.getElementById('round_form_msg').innerHTML = errorValidacion;
                    }
                },
                Close: function() {
                    $(this).dialog("close");
                },
              }
        });		
		
    });
	
	function roundModal(idRound)
    {
        var url_datos = '';
        
        if(idRound == 0) url_datos = 'round/addRound';
        else url_datos = 'round/editRound/'+idRound;

        url_datos = '<?php echo site_url(); ?>'+url_datos;
         $.ajax({
             type: 'POST',
             url: url_datos,
             
             //Cuando se procese con éxito la petición se ejecutará esta función
             success: function(resp) { 
                $('#new_round').dialog("open").html(resp);
             }
         });
    }
</script>

<table width="90%">
	<tr>
    	<td class="menuActual">Config > Rounds</td>
    </tr>
    <tr>
    	<td align="center">
        	<table>
                <tr>
                	<td colspan="6">
					<?php echo form_open('round/getAll'); ?>
                    <?php echo validation_errors();
					
					$msg = '';
					if($startDate != '') $msg = 'Rounds from ' . $startDate .'<br />';
					if($endDate != '') $msg .= ($msg != '' ? 'to ' : 'Rounds to ') . $endDate .'<br />';
					echo $msg;
					?>
                    
                    <table  border="0">
                    	<tr>            
                    		<td class="descripcionForm"><?php echo form_label("From date: ");?></td>
                    		<td class="descripcionForm">
							<?php
                            $attributes = array('id' => 'startDate', 'size' => '10','name' => 'startDate', 'maxlength' => '10', 'readonly' => TRUE,
                                            'onfocus' => "displayCalendar(document.forms[0].startDate,'yyyy/mm/dd',this)", 'placeholder' => '2013/09/23');
                            echo form_input($attributes);
                            ?>
                            </td>
                            <td>&nbsp;</td>         
                            <td class="descripcionForm"><?php echo form_label("To date: ");?></td>
                            <td class="descripcionForm">
                            <?php
                            $attributes = array('id' => 'endDate', 'size' => '10','name' => 'endDate', 'maxlength' => '10', 'readonly' => TRUE,
                                        'onfocus' => "displayCalendar(document.forms[0].endDate,'yyyy/mm/dd',this)", 'placeholder' => '2013/09/23');
                            echo form_input($attributes);
                            ?>
                            </td>
                            <td></td>
                            <td><input type="submit" name="sbEnviar" value="Filter" id="sbEnviar" class="boton"  /></td>
	                    </tr>                    
                    </table>
                    <?php echo form_close(); ?>
                        
                        <div class="scroll-pane-arrows-big-grid">
							<?php if(count($rondas) > 0) { ?>
                            <table width="100%" id="tblRounds" class="tablaJerarquia">
                                <tr>    
                                    <td class="descripcion">Name</td>
                                    <td class="descripcion">Start Date</td>
                                    <td class="descripcion">End Date</td>
                                    <td class="descripcion">Status</td>
                                    <td class="delete">Edit</td>
                                    <td class="delete">Report</td>
                                </tr>                                
                                <tbody>
                                <?php foreach($rondas as $ronda) { ?>
                                    <tr>
                                        <td class="tdDescripcion"><?php echo $ronda->name; ?></td>
                                        <td class="tdDescripcion"><?php echo $ronda->startDate; ?></td>
                                        <td class="tdDescripcion"><?php echo $ronda->endDate; ?></td>
                                        <td class="tdDescripcion">
											<?php 
                                            $ronda_abierta = false;
                                            if($this->Round_dao->roundOpen($ronda->startDate, $ronda->endDate)) {  echo 'OPEN'; $ronda_abierta = true; }
                                            else { echo 'CLOSE'; } ?>
                                        </td>
                                        <td class="delete">
                                            <?php 
                                            if($ronda_abierta) { ?> 
                                                <a href="#" onclick="roundModal(<?=$ronda->id?>)">Edit</a> 
                                            <?php } ?>
                                        </td>
                                        <td class="delete"><a href="<?=site_url('round/downloadReport/' . $ronda->id)?>">Download</a></td>
                                    </tr>
                                <?php } ?>
                                </tbody>
                            </table>
                            <?php } 
							else echo 'No information.';
							?> 
                        </div>
                    </td>            
                </tr>          
            </table>
        </td>
    </tr>
    <tr>
        <td class="footer"><?php if($userIsAdmin) { ?>
               <input onclick="roundModal(0)" value="Add Round" type="button" class="footer_button" />
            <?php } ?>
        </td>            
    </tr>    
</table>


<div class='modal-form' id="new_round">
</div>